package managementSys.question;

import java.util.ArrayList;
import java.util.Scanner;

import javax.naming.InterruptedNamingException;

import org.xml.sax.InputSource;

/**
 * @category 面向对象增删改查
 *
 */
public class QuestionFunction {
	public static void main(String[] args) {
		QuestionFunction qq = new QuestionFunction();	
		qq.start();
	}
	
	Scanner sc = new Scanner(System.in);
	public void start() {
		//题号不能重复
		while(true) {	
			System.out.println("-----------");
			System.out.println("====请选择功能======");
			System.out.println("-----1.试题录入");
			System.out.println("-----2.试题查询");
			System.out.println("-----3.试题修改");
			System.out.println("-----4.试题删除");
			System.out.println("-----5.显示所有试题");
			System.out.println("-----6.根据题号进行排序"); 
			System.out.println("-----7.根据分值进行排序"); 
			System.out.println("-----8.回答试题");		//遍历试题看运行情况 
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				input();
				System.out.println("11111111");
				break;
			case 2:
				getModelById();
				break;
			case 3:
				delModelById();
				break;
			case 4:
				updModelById();
				break;
			case 5:
				print();
				break;
			case 6:
				sortById();
				break;
			case 7:
				sortByValue();
				break;
			case 8:
				test();
				break;
			}
			System.out.println("*********是否退出系统？Y/N");
			if("Y".equalsIgnoreCase(sc.nextLine()))
				break;
			
		}
	}
	public void getModelById() {
		System.out.println("请输入即将查询的题目:");
		int id = Integer.parseInt(sc.nextLine());
		for(QuestionModel model:list) {
			if(model.getId()==id) {
				System.out.println("题号" + model.getId());
				System.out.println("选项A" + model.getOptionA());
				System.out.println("选项B" + model.getOptionB());
				System.out.println("选项C" + model.getOptionC());
				System.out.println("选项D" + model.getOptionD());
				System.out.println("答案" + model.getAnswer());
				System.out.println("分值" + model.getValue());
				return;
			}
		}
		System.out.println("该题号不存在题目");
	}
	public void test() {
		for(QuestionModel model:list) {
			int sum=0;//分数
			int count=0;//个数
			System.out.println("题号" + model.getId());
//			System.out.println("题目" + model.get());
			
			System.out.println("选项A" + model.getOptionA());
			System.out.println("选项B" + model.getOptionB());
			System.out.println("选项C" + model.getOptionC());
			System.out.println("选项D" + model.getOptionD());
			System.out.println("分值" + model.getValue());
			
			System.out.println("请输入答案");
			String line = sc.nextLine();
			if(model.getAnswer().equalsIgnoreCase(line)) {
				sum += model.getValue();
				count++;
			}
		}
		System.out.printf("最终得%d\n分计数%d");
	}
	public void sortById() {							//根据id排序
		int len = list.size();
		for(int i=0;i<len-1;i++) {
			for(int j=1;j<len-i;j++) {
				QuestionModel m1 = list.get(j-1);
				QuestionModel m2 = list.get(j);
				if(m1.getId()>m2.getId()) {
					list.set(j-1,m2);
					list.set(j,m1);
				}
			}
		}
	}
	public void sortByValue() {
		int len = list.size();
		for(int i=0;i<len-1;i++) {
			for(int j=1;j<len-i;j++) {
				QuestionModel m1 = list.get(j-1);
				QuestionModel m2 = list.get(j);
				if(m1.getValue()>m2.getValue()) {
					list.set(j-1,m2);
					list.set(j,m1);
				}
			}
		}
	}
	public void delModelById() {	//for循环删除元素
		System.out.println("请输入即将删除的题目:");
		int id = Integer.parseInt(sc.nextLine());
		for(int i=list.size();i>=0;i--) {
			QuestionModel model = list.get(i);
			if(model.getId() == id) {
				list.remove(i);
				System.out.println("已经删除了题号为"+i);
				return;
			}
		}
		System.out.println("该题号不存在");
	}
	public void updModelById() {
		System.out.println("请输入即将修改的题目:");
		int id = Integer.parseInt(sc.nextLine());	//题号录入
		for (QuestionModel model:list) {
			if(model.getId() == id) {
				System.out.println("所要修改的题目信息如下：");
				System.out.println("题号：" + model.getId());
				System.out.println("选项A：" + model.getOptionA());
				System.out.println("选项B：" + model.getOptionB());
				System.out.println("选项C：" + model.getOptionC());
				System.out.println("选项D：" + model.getOptionD());
				System.out.println("答案：" + model.getAnswer());
				System.out.println("分值：" + model.getValue());
				
			}
		}
	}
	public void inputs() {				//多次输入
		while (true) {
			input();
			System.out.println("是否继续录入?Y/N");
			if("Y".equalsIgnoreCase(sc.nextLine()))
				continue;
		}
	}	
	ArrayList<QuestionModel> list = new ArrayList<QuestionModel>();
	public boolean isExist(int id) {			//判断某个题号在集合中是否存在
		for(QuestionModel model:list) {
			if(model.getId() == id) {
				System.out.println("该题号已存在");
				return true;
			}
		}
		return false;
	}
	public void input() {				//单次输入
		QuestionModel model = new QuestionModel();
		System.out.println("请输入题号：");
		int id = Integer.parseInt(sc.nextLine());
		
		do {										//判重
			System.out.println("请输入题号");
			id = Integer.parseInt(sc.nextLine());
		}while(isExist(id));
		model.setId(id);
		System.out.println("请输入A选项");
		model.setOptionA(sc.nextLine());
		System.out.println("请输入B选项");
		model.setOptionB(sc.nextLine());
		System.out.println("请输入C选项");
		model.setOptionC(sc.nextLine());
		System.out.println("请输入D选项");
		model.setOptionD(sc.nextLine());
		System.out.println("请输入答案");
		model.setAnswer(sc.nextLine());
		System.out.println("请输入分值");
		model.setValue(Integer.parseInt(sc.nextLine()));
		list.add(model);
	}
	public void print() {				//打印所有信息
		for(QuestionModel model:list) {
			System.out.println("所要修改的题目信息如下：");
			System.out.println("题号：" + model.getId());
			System.out.println("选项A：" + model.getOptionA());
			System.out.println("选项B：" + model.getOptionB());
			System.out.println("选项C：" + model.getOptionC());
			System.out.println("选项D：" + model.getOptionD());
			System.out.println("答案：" + model.getAnswer());
			System.out.println("分值：" + model.getValue());
		}
		
	}
}
















